package com.wonderplay.engine;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;

public abstract class GameEngine implements OnTouchListener, OnKeyListener, Callback {

	protected Map<Integer, LogicEngine> moduleEngineMap;
	protected DisplayEngine displayEngine;
	protected ResourceHandler resourceHandler;
	protected MainProcess mainProcess;
	protected SurfaceHolder surfaceHolder;
	protected Context context;
	protected int currentModule;
	
	public GameEngine(Context context){
		this.context = context;
		
		moduleEngineMap = new HashMap<Integer, LogicEngine>();
		displayEngine = new DisplayEngine(context);
		resourceHandler = null;
		surfaceHolder = displayEngine.getHolder();
		mainProcess = new MainProcess(50, this);
		
		displayEngine.setOnTouchListener(this);
		displayEngine.setOnKeyListener(this);
		
		surfaceHolder.addCallback(this);
	}

	abstract public void doStep();
	
	public ResourceHandler getResourceHandler(){
		return resourceHandler;
	}

	public SurfaceHolder getSurfaceHolder(){
		return surfaceHolder;
	}
	
	public void start(){
		mainProcess.start();
	}
	
	public void registerLogicEngine(int id, LogicEngine engine){
		moduleEngineMap.put(id, engine);
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		
	}
	
	abstract public void onModuleChangeReq(int currentModule, int changeCode, ModuleChangeMessage message);
}
